<?

//Initiate Authenticate
SecurityRedirect ();
/*
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
*/
//initiate Vars
define("PRIMARY_KEY","jo_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_jo_costing_appv2.php";
$FileTemplate  = "ais_transaction_jo_costing_appv2_list.html";
$TableName     = "tbljoborder";
$SubHeaderTitle= "Job Costing Approval 2";
$PageSize      = 10 ;
$HTMLGridList  = "ToolGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$jo_code = GetParam("jo_code","");
$projName = GetParam("projName","");
$blName = GetParam("blName","");
$kapalName = GetParam("kapalName",""); 



//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName` as a ";
$SQL.= "JOIN tblproject as b ON a.project_id = b.project_id ";
$SQL.= "JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id ";
$SQL.= "JOIN tbldebtur as d ON a.debtur = d.CUCODE ";
$SQL.= "LEFT JOIN tbljoborder_costing as e ON a.jo_id = e.jo_id ";

if ($jo_code)
    if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$jo_code."%'";
    else $WHERE .= " a.jo_code like '%".$jo_code."%'";

if ($projName)
    if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
    else $WHERE .= " b.project_name like '%".$projName."%'";
	
if ($blName)
    if (strlen($WHERE)) $WHERE .= " AND "." a.bl_no like '%".$blName."%'";
    else $WHERE .= " a.bl_no like '%".$blName."%'";
	
if ($kapalName)
    if (strlen($WHERE)) $WHERE .= " AND "." a.vessel like '%".$kapalName."%'";
    else $WHERE .= " a.vessel like '%".$kapalName."%'";

if (strlen($WHERE)) $WHERE = " WHERE (e.jo_costing_appv_status = 'wait_approved' OR e.jo_costing_appv_status = 'approved_1') AND ".$WHERE;    
    //else $WHERE = " WHERE jo_appv_status='approved' AND jo_costing_appv_status = 'wait_approved' AND jo_costing_appv_status = 'approved_1'";    
    else $WHERE = " WHERE (e.jo_costing_appv_status = 'wait_approved' OR e.jo_costing_appv_status = 'approved_1') ";

$SQL = $SQL.$WHERE." GROUP BY a.jo_id ORDER BY a.jo_id DESC ";




//COUNT

$WHERE = "";

$SQLCount = "SELECT count(*) FROM `$TableName` as a ";
$SQLCount.= "JOIN tblproject as b ON a.project_id = b.project_id ";
$SQLCount.= "JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id ";
$SQLCount.= "JOIN tbldebtur as d ON a.debtur = d.CUCODE ";
$SQLCount.= "LEFT JOIN tbljoborder_costing as e ON a.jo_id = e.jo_id ";

if ($jo_code)
    if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$jo_code."%'";
    else $WHERE .= " a.jo_code like '%".$jo_code."%'";

if ($projName)
    if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
    else $WHERE .= " b.project_name like '%".$projName."%'";
	
if ($blName)
    if (strlen($WHERE)) $WHERE .= " AND "." a.bl_no like '%".$blName."%'";
    else $WHERE .= " a.bl_no like '%".$blName."%'";
	
if ($kapalName)
    if (strlen($WHERE)) $WHERE .= " AND "." a.vessel like '%".$kapalName."%'";
    else $WHERE .= " a.vessel like '%".$kapalName."%'";

if (strlen($WHERE)) $WHERE = " WHERE (e.jo_costing_appv_status = 'wait_approved' OR e.jo_costing_appv_status = 'approved_1') AND ".$WHERE;    
    //else $WHERE = " WHERE jo_appv_status='approved' AND jo_costing_appv_status = 'wait_approved' AND jo_costing_appv_status = 'approved_1'";    
    else $WHERE = " WHERE (e.jo_costing_appv_status = 'wait_approved' OR e.jo_costing_appv_status = 'approved_1') ";

$SQLCount = $SQLCount.$WHERE." GROUP BY a.jo_id ORDER BY a.jo_id DESC ";




//Select Department
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName,$TableName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("$TableName",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

?>
